package com.jiliang.jiliangserver.service.impl;

import com.jiliang.jiliangserver.domain.*;
import com.jiliang.jiliangserver.mapper.MaterialMapper;
import com.jiliang.jiliangserver.model.AjaxResult;
import com.jiliang.jiliangserver.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 物资服务类
 */
@Service
public class MaterialServiceImpl implements MaterialService {

    @Autowired
    private MaterialMapper materialMapper;
    /**
     * 创建电池
     * @param battery
     */
    @Override
    public void createBattery(Battery battery){
        int i = materialMapper.createBattery(battery);
        if(i<=0){
            throw new RuntimeException("创建失败");
        }

    }

    /**
     * 创建品牌
     * @param brand
     */
    @Override
    public void createBrand(Brand brand) {
        int i = materialMapper.createBrand(brand);
        if(i<=0){
            throw new RuntimeException("创建失败");
        }

    }

    @Override
    public Brand selectBrandById(Long id) {
        Brand brand = materialMapper.selectBrandById(id);
        return brand;
    }

    @Override
    public List<Brand> selectBrandList(Brand brand) {
        List<Brand> brands = materialMapper.selectBrandList(brand);
        return brands;
    }

    @Override
    public Battery selectBatteryById(Long id) {
        Battery battery = materialMapper.selectBatteryById(id);
        return battery;
    }

    @Override
    public List<Battery> selectBatteryList(Battery battery) {
        List<Battery> batteries = materialMapper.selectBatteryList(battery);
        return batteries;
    }

    @Transactional
    @Override
    public void changeBattery(MaterialLog log) {

        Battery oldBt = new Battery();
        oldBt.setId(log.getOldmaterialid());
        oldBt.setBatteryStatus(log.getOldmaterialstatus());
        Battery newBt = new Battery();
        newBt.setId(log.getNewmaterialid());
        newBt.setUseTime(new Date());
        newBt.setBatteryStatus(log.getNewmaterialstatus());
        int i1 = materialMapper.updateBattery(oldBt);
        int i2 = materialMapper.updateBattery(newBt);

        log.setChangetime(new Date());
        int i3 = materialMapper.createMaterialLog(log);
    }

    @Override
    public List<Map> selectBatteryBrandCounts(Battery battery) {
        return materialMapper.selectBatteryBrandCounts(battery);
    }

    @Override
    public int createJlModel(JlModel jlModel) {
        int jlModel1 = materialMapper.createJlModel(jlModel);
        return jlModel1;
    }

    @Override
    public List<JlModel> selectJlModelList(JlModel jlModel) {
        List<JlModel> jlModels = materialMapper.selectJlModelList(jlModel);
        return jlModels;
    }

    @Override
    public JlModel selectJlModelById(Long id) {
        return materialMapper.selectJlModelById(id);
    }

    @Override
    public int updateJlModel(JlModel jlModel) {
        int jlModel1 = materialMapper.updateJlModel(jlModel);
        return jlModel1;
    }

    @Override
    public Simcard selectSimcardById(Long simcardId) {
        Simcard simcard = materialMapper.selectSimcardById(simcardId);
        return simcard;
    }

    @Override
    public int createSimcard(Simcard simcard) {
        int i = materialMapper.createSimcard(simcard);
        return i;
    }

    @Override
    public List<Simcard> selectSimcardList(Simcard simcard) {
        List<Simcard> simcards = materialMapper.selectSimcardList(simcard);
        return simcards;
    }

    @Override
    public int updateSimcard(Simcard simcard) {
        int i = materialMapper.updateSimcard(simcard);
        return i;
    }

    @Override
    public int createWaterUse(WaterUse waterUse) {
        return materialMapper.createWaterUse(waterUse);
    }

    @Override
    public List<WaterUse> selectWaterUseList(WaterUse waterUse) {
        return materialMapper.selectWaterUseList(waterUse);
    }

    @Override
    public int updateWaterUse(WaterUse waterUse) {
        return materialMapper.updateWaterUse(waterUse);
    }

    @Override
    public WaterUse selectWaterUseById(Long id) {
        return materialMapper.selectWaterUseById(id);
    }

    @Override
    public int createYcTemplate(YcTemplate ycTemplate) {
        return materialMapper.createYcTemplate(ycTemplate);
    }

    @Override
    public List<YcTemplate> selectYcTemplateList(YcTemplate ycTemplate) {
        return materialMapper.selectYcTemplateList(ycTemplate);
    }

    @Override
    public int updateYcTemplate(YcTemplate ycTemplate) {
        return materialMapper.updateYcTemplate(ycTemplate);
    }

    @Override
    public YcTemplate selectYcTemplateById(Long id) {
        return materialMapper.selectYcTemplateById(id);
    }


}
